package cn.edu.tsinghua.cic.ccss.web;

import java.util.List;

import javax.annotation.Resource;
import javax.mail.internet.AddressException;

import org.forten.core.ro.DtoResultWithPageInfo;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.edu.tsinghua.cic.ccss.bo.CadreBo;
import cn.edu.tsinghua.cic.ccss.bo.CourseBo;
import cn.edu.tsinghua.cic.ccss.bo.CourseSelectBo;
import cn.edu.tsinghua.cic.ccss.dto.CadreDto;
import cn.edu.tsinghua.cic.ccss.dto.CourseCanceledDto;
import cn.edu.tsinghua.cic.ccss.dto.CourseDto;
import cn.edu.tsinghua.cic.ccss.dto.CourseSelectionDto;
import cn.edu.tsinghua.cic.ccss.qo.CadreListQo;
import cn.edu.tsinghua.cic.ccss.qo.CourseSelectAdminQo;

/**管理端选课Controller
 * @author cj
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("selectcourse")
@SessionAttributes({ "courseSelectAdminQo", "cadreListQo", "courseId" })
public class CourseAction4SelectionAdmin extends SuperAction {

	@Resource(name = "courseSelectAdminQo")
	private CourseSelectAdminQo courseSelectAdminQo;
	@Resource(name = "courseBo")
	private CourseBo courseBo;
	@Resource(name = "cadreBo")
	private CadreBo cadreBo;
	@Resource(name = "courseSelectBo")
	private CourseSelectBo courseSelectBo;
	@Resource(name = "cadreListQo")
	private CadreListQo cadreListQo;
	private String message = null;

	@RequestMapping("gotoCourseSelectionAdmin")
	public String gotoCourseSelectionAdmin(Model model) {
		model.addAttribute("courseSelectAdminQo", courseSelectAdminQo);
		return "forward:showCourseSelectionAdminList.jspx";
	}

	@RequestMapping("showCourseSelectionAdminList")
	public String showCourseSelectionAdmin(
			Model model,
			@RequestParam(defaultValue = "1") int pageNo,
			@ModelAttribute("courseSelectAdminQo") CourseSelectAdminQo courseSelectAdminQo) {
		DtoResultWithPageInfo<CourseDto> dtoList = this.courseBo
				.queryCourseDtoPage(courseSelectAdminQo, pageNo,"teachTime",false);
		model.addAttribute("courseSelectAdminQo", courseSelectAdminQo);
		model.addAttribute("dto", (List<CourseDto>) dtoList.getResults());
		model.addAttribute("page", dtoList.getPageInfo());
		return "selectcourse/gldxk_kkxxList";
	}

	@RequestMapping("gotoCadreList")
	public String gotoCadreList(Model model,
			@RequestParam(defaultValue = "") Long courseId) {
		model.addAttribute("cadreListQo", cadreListQo);
		model.addAttribute("courseId", courseId);
		return "forward:showCadreList.jspx";
	}

	@RequestMapping("showCadreList")
	public String showCadreList(Model model,
			@ModelAttribute("courseId") Long courseId,
			@ModelAttribute("cadreListQo") CadreListQo cadreListQo,
			@RequestParam(defaultValue = "1") int pageNo) {
		DtoResultWithPageInfo<CadreDto> dtos = this.cadreBo.queryCadreDtoPage(
				cadreListQo, pageNo);
		// DtoResultWithPageInfo<CadreDto> dtos = this.cadreBo
		// .queryCadreDtoPage4Selection(cadreListQo, pageNo);
		CourseDto dtoCourse = this.courseBo.loadCourseDto(courseId);
		model.addAttribute("dtoCourse", dtoCourse);
		model.addAttribute("dto", dtos.getResults());
		model.addAttribute("page", dtos.getPageInfo());
		model.addAttribute("cadreListQo", cadreListQo);
		return "selectcourse/gldxk_gbxz";
	}

	@RequestMapping("gotoCourseSelectionResults")
	public String gotoCourseSelectionResults(Model model,
			@ModelAttribute("courseId") Long courseId, @RequestParam Long[] pid) {
		this.courseSelectBo
				.selectCourse4Admin(courseId, getLoginCardNum(), pid);
		message = "添加选课成功！";
		List<CourseSelectionDto> dto = this.courseSelectBo
				.queryStudentsForThisCourse(courseId);
		List<CourseCanceledDto> dto_cancelCourse = this.courseSelectBo
				.queryStudentsForThisCourseWithCancelInfo(courseId);
		CourseDto dtoCourse = this.courseBo.loadCourseDto(courseId);
		model.addAttribute("dtoCourse", dtoCourse);		
		model.addAttribute("dto_cancelCourse", dto_cancelCourse);
		model.addAttribute("message", message);
		model.addAttribute("dto", dto);
		return "selectcourse/gldxk_xkxx";
	}

	@RequestMapping("cancelCourseByAdmin")
	public String cancelCourse(Model model,
			@ModelAttribute("courseId") Long courseId,
			@RequestParam Long selectedCourseId,
			@RequestParam String cancelReason) {
		this.courseSelectBo.cancelCourse(selectedCourseId, cancelReason);
		message = "退课成功";
		List<CourseSelectionDto> dto = this.courseSelectBo
				.queryStudentsForThisCourse(courseId);
		List<CourseCanceledDto> resultList = this.courseSelectBo
				.queryStudentsForThisCourseWithCancelInfo(courseId);
		model.addAttribute("dto_cancelCourse", resultList);
		model.addAttribute("dto", dto);
		model.addAttribute("message", message);
		return "selectcourse/gldxk_xkxx";
	}

	/*
	 * @RequestMapping("leaveByAdmin") public void leaveByAdmin(Model model,
	 * @RequestParam Long selectedCourseId,
	 * 
	 * @RequestParam String state, @RequestParam String leaveReason) {
	 * this.courseSelectBo.leave4Admin(selectedCourseId, state, leaveReason); }
	 */

	@RequestMapping("gotoSendMailToCadre")
	public String gotoSendMailToCadre(Model model, @RequestParam Long courseId) {
		String msg = null;
		try {
			msg = courseBo.getMailMessageToCadre(courseId);
		} catch (AddressException e) {

		}
		model.addAttribute("msg", msg);
		model.addAttribute("courseId", courseId);
		return "selectcourse/edit_mail";
	}

	@RequestMapping("sendMail")
	public String sendMail(Model model,
			@ModelAttribute("courseId") Long courseId, @RequestParam String msg) {
		this.courseBo.sendMail(courseId, msg);
		message = "邮件发送成功！";
		model.addAttribute("message", message);
		return "selectcourse/edit_mail";
	}

	@RequestMapping("gotoSendShortMessageToCadre")
	public String gotoSendShortMessageToCadre(Model model,
			@RequestParam Long courseId) {
		String msg = courseBo.getShortMessageToCadre(courseId);
		model.addAttribute("msg", msg);
		model.addAttribute("courseId", courseId);
		return "selectcourse/edit_shortmsg";
	}

	@RequestMapping("sendShortMessage")
	public String sendShortMessage(Model model,
			@ModelAttribute("courseId") Long courseId, @RequestParam String msg) {
		this.courseBo.sendShortMessage(courseId, msg);
		message = "短信发送成功！";
		model.addAttribute("message", message);
		return "selectcourse/edit_shortmsg";
	}
}